const { ccclass, property } = cc._decorator;

@ccclass
export default class Game extends cc.Component {

    @property(cc.Node)
    mainBox: cc.Node = null;

    @property(cc.Label)
    timeLabel: cc.Label = null;

    @property(cc.Prefab)
    number: cc.Prefab = null;

    startData: number[] = [];
    residueData: number[] = [];

    numberBoxList = null;

    cur_num = 1;

    time = 0

    onLoad() {
        this.mainBox.width = cc.winSize.width - 40
        this.mainBox.height = this.mainBox.width

        this.timeLabel.node.setPosition(-cc.winSize.width / 2 + this.timeLabel.node.width / 2 + 20, cc.winSize.height / 2 - this.timeLabel.node.height / 2 - 20)

        this.get_start_initData();
        var index = 0;
        for (var y = 0; y < 5; y++) {
            for (var x = 0; x < 5; x++) {
                var numberBox = cc.instantiate(this.number)
                numberBox.getComponent("NumberBox").game = this

                numberBox.getComponent("NumberBox").set_text(this.startData[index]);
                numberBox.width = (this.mainBox.width - 30) / 5
                numberBox.height = (this.mainBox.height - 30) / 5
                var n_x = 5 + (5 + numberBox.width) * x + numberBox.width / 2
                var n_y = 5 + (5 + numberBox.height) * y + numberBox.height / 2
                numberBox.scaleX = 0
                numberBox.scaleY = 0
                numberBox.setPosition(n_x, n_y + cc.winSize.height);

                //numberBox.setPosition(n_x, n_y);

                var action =
                    cc.spawn(
                        cc.scaleTo(0.05*index+0.1, 1, 1),
                        cc.moveTo(0.05*index+0.1, cc.v2(n_x, n_y)).easing(cc.easeCircleActionIn())
                    )


                numberBox.runAction(action)
  
                


                this.numberBoxList[x][y] = numberBox

                numberBox.getComponent("NumberBox").setArrPosition(x, y)

                this.mainBox.addChild(numberBox)
                index++;
            }
        }



    }

    number_click(x, y) {
        var numberBox = this.numberBoxList[x][y]
        var c_num = numberBox.getComponent("NumberBox").num
        if (c_num == this.cur_num) {
            var num = this.residueData[0]
            if (num) {
                numberBox.getComponent("NumberBox").set_text(num);
                this.residueData.shift()
            } else {
                this.mainBox.removeChild(numberBox)
            }
            if (c_num == 1) {
                this.start_game()
            }
            if (c_num == 50) {
                this.end_game()
            }
            this.cur_num++;
        } else {
            cc.log("点击数字不对")
        }

    }

    start_game() {
        this.schedule(this.time_add, 0.001)
    }

    end_game() {
        this.unschedule(this.time_add)
        this.mainBox.runAction(cc.hide())
        this.timeLabel.node.runAction(cc.spawn(
            cc.moveTo(1, 0, 0),
            cc.scaleTo(1, 1)
        ));
    }
    time_add() {
        this.time++;
        this.timeLabel.string = (this.time / 60).toFixed(3);
    }

    get_start_initData() {
        var arr: number[] = [];
        for (var num = 1; num <= 25; num++) {
            arr.push(num)
        }
        this.startData = arr.sort(this.randomsort)
        var arr: number[] = [];
        for (var num = 26; num <= 50; num++) {
            arr.push(num)
        }
        this.residueData = arr.sort(this.randomsort)
        // 初始化方块数组
        this.numberBoxList = [
            [null, null, null, null, null],
            [null, null, null, null, null],
            [null, null, null, null, null],
            [null, null, null, null, null],
            [null, null, null, null, null]
        ];

    }
    randomsort(a, b) {
        return Math.random() > .5 ? -1 : 1;
    }
}
